<template>
  <div class="addContainer">
    <div
      style="
        height: 100%;
        padding-left: 150px;
        padding-right: 150px;
        overflow-y: auto;
        padding-bottom: 70px;
      "
    >
      <div style="padding: 20px">
        <el-card>
          <template #header>
            <span style="font-weight: bold; font-size: 16px"
              >新增供应商退货</span
            >
          </template>
          <div class="addBody">
            <div class="basicInfoContainer">
              <div class="milestone">
                <div class="milestone-content">基础信息</div>
              </div>
              <el-form
                style="margin-top: 40px"
                label-width="120"
                ref="editFormRef"
                :model="editForm"
                :rules="rules"
              >
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="退货单编号" prop="returnId">
                      <el-input
                        placeholder="请输入"
                        style="width: 300px"
                        v-model.trim="editForm.returnId"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="退货单名称" prop="returnName">
                      <el-input
                        placeholder="请输入"
                        style="width: 300px"
                        v-model.trim="editForm.returnName"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="退货日期" prop="returnDate">
                      <el-date-picker
                        style="width: 300px"
                        type="date"
                        placeholder="请选择日期"
                        v-model.trim="editForm.returnDate"
                      ></el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="采购单名称" prop="purchaseName">
                      <el-input
                        placeholder="请输入"
                        style="width: 300px"
                        v-model.trim="editForm.purchaseName"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="采购单号" prop="purchaseId">
                      <el-input
                        placeholder="请输入"
                        style="width: 300px"
                        v-model.trim="editForm.purchaseId"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="供应商" prop="supplierName">
                      <el-input
                        placeholder="请输入"
                        style="width: 300px"
                        v-model.trim="editForm.supplierName"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="退货总数" prop="returnNum">
                      <el-input-number
                        placeholder="请输入"
                        style="width: 300px"
                        v-model.number="editForm.returnNum"
                      ></el-input-number>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="备注" prop="remark">
                  <el-input
                    type="textarea"
                    style="width: 795px"
                    v-model="editForm.remark"
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
            <div class="bomContainer">
              <div class="bomInfoContainer">
                <div class="milestone">
                  <div class="milestone-content">退货明细</div>
                </div>
                <div class="buttonGroup" style="margin-top: 20px">
                  <el-button type="primary" style="width: 80px">添加</el-button>
                  <el-button type="primary" style="width: 80px" plain
                    >移除</el-button
                  >
                </div>
                <el-table border style="margin-top: 20px">
                  <el-table-column
                    type="selection"
                    align="center"
                    fixed="left"
                  ></el-table-column>
                  <el-table-column
                    type="index"
                    align="center"
                    label="序号"
                    width="60"
                    fixed="left"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="物料名称"
                    width="90"
                    fixed="left"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="物料编号"
                    width="90"
                    fixed="left"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="规格型号"
                    width="90"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="单位"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="批次号"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="退货数量"
                    width="90"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="仓库名称"
                    width="90"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="库区名称"
                    width="90"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="库位名称"
                    width="90"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="负责人"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="备注"
                    width="200"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    label="操作"
                    width="100"
                    fixed="right"
                  ></el-table-column>
                </el-table>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <div class="footer" :class="{ 'footer-visible': isFooterVisible }">
      <div class="footerButton">
        <el-button type="primary" plain style="width: 80px" @click="close"
          >关闭</el-button
        >
        <el-button type="primary" style="width: 80px" @click="confirmUpdate"
          >确定</el-button
        >
      </div>
    </div>
  </div>
</template>
        
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { getSupplierDetailApi, putSupplierUpdateApi } from "@/api/Supplier";
import { ElMessage } from "element-plus";
import { SupplierData } from "@/api/Supplier/res";
import { SupplierAddData } from "@/api/Supplier/param";
import dayjs from "dayjs";

const $router = useRouter();
const $route = useRoute();
const editFormRef = ref(null);
const isFooterVisible = ref(false);
const rules = {
  returnId: [{ required: true, message: "请输入退货单编号", trigger: "blur" }],
  returnName: [
    { required: true, message: "请输入退货单名称", trigger: "blur" },
  ],
  returnDate: [{ required: true, message: "请输入退货日期", trigger: "blur" }],
  purchaseName: [
    { required: true, message: "请输入采购单名称", trigger: "blur" },
  ],
  purchaseId: [{ required: true, message: "请输入采购单号", trigger: "blur" }],
  supplierName: [{ required: true, message: "请输入供应商", trigger: "blur" }],
  returnNum: [{ required: true, message: "请输入退货总数", trigger: "blur" }],
  remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
};
let editForm = ref<SupplierData>({
  remark: "",
  returnDate: "",
  returnId: "",
  returnName: "",
  purchaseId: "",
  purchaseName: "",
  returnNum: "",
  supplierName: "",
  objectId: "",
});

onMounted(async () => {
  setTimeout(() => {
    isFooterVisible.value = true;
  }, 100);
  try {
    const res: SupplierData = await getSupplierDetailApi(
      $route.query.objectId as string
    );
    editForm.value = res;
  } catch (err: any) {
    ElMessage({ type: "error", message: err.message });
  }
});

const close = () => {
  $router.replace("/storage/entrance/supplier");
};
const confirmUpdate = () => {
  editFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const updateData = Object.keys(editForm.value).reduce((obj, key) => {
          if (key !== "createdAt") {
            obj[key] = editForm.value[key];
          }
          return obj;
        }, {} as SupplierAddData);

        // 格式化日期
        updateData.returnDate = dayjs(editForm.value.returnDate).format(
          "YYYY-MM-DD"
        );
        updateData.returnNum = String(editForm.value.returnNum);
        const res: any = await putSupplierUpdateApi(updateData);
        ElMessage({ type: "success", message: "编辑成功" });
        close();
      } catch (err: any) {
        ElMessage({ type: "error", message: err.message });
      }
    } else {
      ElMessage({ type: "error", message: "请先填写必要字段" });
    }
  });
};
</script>
        <style scoped lang="scss">
.addContainer {
  width: 100%;
  position: relative;
  height: 100%;
  transform: translate(0);
  .addBody {
    width: 100%;
    .basicInfoContainer,
    .bomInfoContainer {
      padding: 20px;
      .milestone {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 1.5px;
        background-color: #dfe2e7;
        .milestone-content {
          border: 1.5px solid #dfe2e7;
          padding: 10px;
          padding-left: 20px;
          padding-right: 20px;
          font-weight: bold;
          font-size: 15px;
          border-radius: 10px;
          z-index: 999;
          background-color: #fff;
          color: #1b9aee;
        }
      }
    }
    .bomInfoContainer {
      margin-top: 20px;
    }
  }
  .footer {
    z-index: 999;
    width: 100%;
    height: 70px;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 10px;
    display: flex;
    justify-content: center;
    box-shadow: 0 -8px 10px rgba(92, 92, 92, 0.1);
    transform: translateY(100%); // 初始状态在视口下方
    transition: transform 0.5s ease-out; // 添加过渡效果

    &.footer-visible {
      transform: translateY(0); // 显示时回到原位
    }
  }
}
</style>